$(document).ready(function(){

    /*  AGENCY NEWS SYSTEM ID 
    *   NEWS COUNTER
    *   NEWS DATA
    *   NEWS TOTAL
    *   NEWS SYSTEM URL 
    */
    var LS_ac_id = 32;
    var cnt=0;
    var news
    var total;
    var url = "http://www.logicsolution.rs/logicsol_news/";

    function citaj_podatke(){
        jQuery.ajax({
            type: 'GET',
            url: url+'onsite_news/citaj_podatke/'+LS_ac_id+'/?jsoncall=?',
            dataType: 'jsonp',
            success: function(data){
                news = data;
                total = news['jsonp'].length;
                populate();
            },
            error:function(data){alert("Error: " + data);}
        });
        return false;
    }
    function prikazi_vest(id){
        id = parseInt(id);
        /*NEWS LIKE SLIDER LOGIC*/
        if(cnt+id>total){
            cnt=1;    
        }else if(cnt+id==total){
            cnt=0; 
        }else cnt+= id;
        //console.log("%s is curent id of %d total" , cnt, total);
        populate();
    }
    function populate(){
        jQuery('#main-news .title h2').html(news['jsonp'][cnt].title);
        jQuery('#main-news .text p').html(news['jsonp'][cnt].text);
        if(news['jsonp'][cnt].foto_path==""){
            jQuery('#main-news .image').html("")    
        }else{
            jQuery('#main-news .image').html('<img width="274" height="190" src="'+url+'assets/upload/fotografije/'+news['jsonp'][cnt].foto_path+'">');
        }

        /*NEWS LIKE SLIDER LOGIC*/
        fixed = cnt+1;
        if(fixed==total) fixed = 0;

        /*TEXT LOGIC - CUT*/
        var title;
        var text;
        title = news['jsonp'][fixed].title;
        text = news['jsonp'][fixed].text;

        if(title.length>58) title = title.substring(0, 57)+'...';    
        if(text.length>127) text = text.substring(0, 125)+'...';    

        jQuery('#link-news-1 #one-news .title h2').html(title); 
        jQuery('#link-news-1 #one-news .text p').html(text);
        if(news['jsonp'][fixed].foto_path==""){
            jQuery('#link-news-1 #one-news .image').html("")    
        }else{
            var clean = news['jsonp'][fixed].foto_path.substring(0,news['jsonp'][fixed].foto_path.length-4);
            var thumb_foto_path = clean +"_90x65.jpg";
            jQuery('#link-news-1 #one-news .image').html('<img width="90" height="65" src="'+url+'assets/upload/fotografije/'+thumb_foto_path+'">');;
        }

        /*NEWS LIKE SLIDER LOGIC*/
        fixed=fixed+1;
        if(fixed==total) fixed = 0;

        title = news['jsonp'][fixed].title;
        text = news['jsonp'][fixed].text;

        if(title.length>58) title = title.substring(0, 57)+'...';    
        if(text.length>127) text = text.substring(0, 125)+'...';    

        jQuery('#link-news-2 #one-news .title h2').html(title); 
        jQuery('#link-news-2 #one-news .text p').html(text);
        if(news['jsonp'][fixed].foto_path==""){
            jQuery('#link-news-2 #one-news .image').html("")    
        }else{
            var clean = news['jsonp'][fixed].foto_path.substring(0,news['jsonp'][fixed].foto_path.length-4);
            var thumb_foto_path = clean +"_90x65.jpg";
            jQuery('#link-news-2 #one-news .image').html('<img width="90" height="65" src="'+url+'assets/upload/fotografije/'+thumb_foto_path+'">');
        }
    }
    jQuery('.link-news').live('click',function(){
        var id = this.id.substring(10, this.id.length);
        prikazi_vest(id);

    })
    citaj_podatke();
});